home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_mac.hqx / SRGP port to 5.0 (compressed) / SRGP_SPHIGS Root / MacSPHIGS / MAT3mat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-13  |  2.7 KB  |  132 lines

  1. #include "HEADERS.h"
  2. /* Copyright 1988, Brown Computer Graphics Group.  All Rights Reserved. */
  3.  
  4. /* --------------------------------------------------------------------------
  5.  * This file contains routines that operate solely on matrices.
  6.  * -------------------------------------------------------------------------*/
  7.  
  8. #include "mat3defs.h"
  9.  
  10. #include "macros.h"
  11.  
  12. #include "MAT3mat.proto.h"
  13.  
  14. /* --------------------------  Static Routines    ---------------------------- */
  15.  
  16. /* -------------------------  Internal Routines  --------------------------- */
  17.  
  18. /* --------------------------  Public Routines    ---------------------------- */
  19.  
  20.  
  21. /*
  22.  * Sets a matrix to identity.
  23.  */
  24.  
  25. void
  26. MAT3identity (register MAT3mat mat)
  27. {
  28.    register int i;
  29.  
  30.    bzero (mat, sizeof(MAT3mat));
  31.    for (i = 0; i < 4; i++)
  32.       mat[i][i] = 1.0;
  33. }
  34.  
  35. /*
  36.  * Sets a matrix to zero.
  37.  */
  38.  
  39. void
  40. MAT3zero (MAT3mat mat)
  41. {
  42.    bzero (mat, sizeof(MAT3mat));
  43. }
  44.  
  45.  
  46. /*
  47.  * Copies one matrix to another.
  48.  */
  49.  
  50. void
  51. MAT3copy(MAT3mat to, MAT3mat from)
  52. {
  53.    bcopy (from, to, sizeof(MAT3mat));
  54. }
  55.  
  56. /*
  57.  * This multiplies two matrices, producing a third, which may the same as
  58.  * either of the first two.
  59.  */
  60.  
  61. void
  62. MAT3mult (MAT3mat result_mat, MAT3mat mat1, MAT3mat mat2)
  63. {
  64.    register int i, j;
  65.    MAT3mat    tmp_mat;
  66.  
  67.    for (i = 0; i < 4; i++)
  68.       for (j = 0; j < 4; j++)
  69.          tmp_mat[i][j] = (mat1[i][0] * mat2[0][j] +
  70.                mat1[i][1] * mat2[1][j] +
  71.                mat1[i][2] * mat2[2][j] +
  72.                mat1[i][3] * mat2[3][j]);
  73.    MAT3copy (result_mat, tmp_mat);
  74. }
  75.  
  76. /*
  77.  * This returns the transpose of a matrix.  The result matrix may be
  78.  * the same as the one to transpose.
  79.  */
  80.  
  81. void
  82. MAT3transpose (MAT3mat result_mat, MAT3mat mat)
  83. {
  84.    register int i, j;
  85.    MAT3mat    tmp_mat;
  86.  
  87.    for (i = 0; i < 4; i++) 
  88.       for (j = 0; j < 4; j++) 
  89.          tmp_mat[i][j] = mat[j][i];
  90.  
  91.    MAT3copy (result_mat, tmp_mat);
  92. }
  93.  
  94.  
  95. /*
  96.  * This prints the given matrix to the given file pointer.
  97.  */
  98.  
  99. void
  100. MAT3print(MAT3mat mat, FILE *fp)
  101. {
  102.    MAT3print_formatted(mat, fp, CNULL, CNULL, CNULL, CNULL);
  103. }
  104.  
  105. /*
  106.  * This prints the given matrix to the given file pointer.
  107.  * use the format string to pass to fprintf.  head and tail
  108.  * are printed at the beginning and end of each line.
  109.  */
  110.  
  111. void
  112. MAT3print_formatted(MAT3mat mat, FILE *fp, char *title, char *head, char *format, char *tail)
  113. {
  114.    register int i, j;
  115.  
  116.    /* This is to allow this to be called easily from a debugger */
  117.    if (fp == NULL) fp = stderr;
  118.  
  119.    if (title  == NULL)    title  = "MAT3 matrix:\n";
  120.    if (head   == NULL)    head   = "  ";
  121.    if (format == NULL)    format = "%#8.4lf  ";
  122.    if (tail   == NULL)    tail   = "\n";
  123.  
  124.    (void) fprintf(fp, title);
  125.  
  126.    for (i = 0; i < 4; i++) {
  127.       (void) fprintf(fp, head);
  128.       for (j = 0; j < 4; j++) (void) fprintf(fp, format, mat[i][j]);
  129.       (void) fprintf(fp, tail);
  130.    }
  131. }
  132.